package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.SendSmsUtils;
import com.itheima.reggie.utils.SendSmsUtils1;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
//    发送验证码
    @PostMapping("/sendMsg")
    public Result<String> sendMsg(@RequestBody User user, HttpServletRequest httpServletRequest) throws Exception {
        String phone = user.getPhone();
        if (!phone.isEmpty()) {
            String param = ValidateCodeUtils.generateValidateCode(6).toString();
            //SendSmsUtils1.sendSms1("张耀辉的项目", "SMS_464110718",phone,param);
            log.info("验证码为：{}", param);
//            httpServletRequest.getSession().setAttribute(phone, param);
//            将生成的验证码缓存到Redis中，并设置有效期为5分钟
            redisTemplate.opsForValue().set(phone,param,5L, TimeUnit.MINUTES);
            return Result.success("短信发送成功");
        }
        return Result.error("短信发送失败");
    }

    //    用户登录
    @PostMapping("/login")
    public Result<User> login(@RequestBody Map map,HttpServletRequest request){
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
//        String rightCode = request.getSession().getAttribute(phone).toString();
//        从Redis中获取缓存的验证码
        String rightCode = redisTemplate.opsForValue().get(phone).toString();
//        验证码错误直接返回错误数据
        if(rightCode==null||!code.equals(rightCode)){
            return Result.error("登陆失败");
        }
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper();
        lqw.eq(User::getPhone,phone);
        User user = userService.getOne(lqw);
//        如果电话号码未注册，先注册
        if(user==null){
            user=new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }
//        注册了直接放行
        request.getSession().setAttribute("user",user.getId());
//        如果用户登陆成功，则删除Redis中缓存的验证码
        redisTemplate.delete(phone);
        return Result.success(user);
    }
//    用户退出
    @PostMapping("/loginout")
    public Result<String> logout(HttpServletRequest request){
        request.removeAttribute("user");
        return Result.success("用户退出成功");
    }
}
